<?php


namespace app\admin\controller;
use app\common\lib\Auth;
use think\Db;
use think\Request;

class Menu extends Base
{
    public function rule_index()
    {
        if(request()->isAjax()){
            extract(input()); //将接收的参数赋值给变量
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $map = array();
            //关键字查询
            try {
                if (isset($keyword) && $keyword != "") {
                    $count = Db::name('auth_rule')->where($map)->count();  //总条目数据
                    $lists = Model('AuthRule')->ajax_tree($NowPage, $limits, 'title', $keyword);  //当前页
                }else {
                    $count = Db::name('auth_rule')->count();  //总条目数据
                    $lists = Model('AuthRule')->ajax_tree($NowPage, $limits, 'title', "");  //当前页
                }
            }catch (\Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage()]);
            }
            return json(['code' => 200, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }else {

            /*************************检测权限 开始***********************/
            $auth = new Auth();
            $request = Request::instance();
            if(!$auth->check($request->path(),$this->isLogin()->id)) {
                return $this->redirect('not_rule/index/index');
            }
            /*************************检测权限 结束***********************/

            return $this->fetch();
        }
    }

    public function rule_index_add()
    {
        if(request()->isAjax()){
            $data = input('post.');
            $Plevel = Model('AuthRule')->getPlevel($data['pid']);
            if($Plevel) {
                $data['level'] = $Plevel['0'] + 1;
            }else{
                $data['level'] = 1;
            }
            try{
                $_add_res = Model('AuthRule')->save($data);
            }catch (\Exception $e) {
                return show('200',$e->getMessage(),'0');
            }
            if(!$_add_res) {
                return show('200','数据库错误','0');
            }
            return show('200','添加成功，刷新父页面生效','1');
        }else {
            $auth_rule = Model('AuthRule')->getFirstNav();
            $this->assign([
                'auth_rule' => $auth_rule,
            ]);
            $this->rule_index();
            return $this->fetch();
        }
    }

    public function rule_index_edit()
    {
        if(request()->isAjax()) {
            $data = input('post.');
            $Plevel = Model('AuthRule')->getPlevel($data['pid']);
            if($Plevel) {
                $data['level'] = $Plevel['0'] + 1;
            }else{
                $data['level'] = 1;
            }
//            return show('200','ok',$data);
            try{
                $_add_res = Model('AuthRule')->save($data,['id'=>$data['id']]);
            }catch (\Exception $e) {
                return show('200',$e->getMessage(),'0');
            }
            if(!$_add_res) {
                return show('200','数据库错误','0');
            }
            return show('200','修改成功，刷新父页面生效','1');
        }else{
            $id = Request::instance()->param('id');
            $this_data = Model('AuthRule')->get(['id'=>$id]);
            $this->assign([
                'this_data' => $this_data
            ]);
            $this->rule_index_add();
            $this->rule_index();
            return $this->fetch();
        }
    }

    public function rule_index_del()
    {
        if(request()->isAjax()) {
            $id = Request::instance()->param('id');
            try{
                $res = Model('AuthRule')->del_msg($id);
            }catch (\Exception $e){
                return show('200',$e->getMessage(),'0');
            }
            if (!$res) {
                return show('200','数据库错误','0');
            }
            return show('200','删除成功！','1');
        }else{
            $this->rule_index();
            return show('500','非法操作');
        }
    }

    public function group_index()
    {
        if(request()->isAjax()){
            extract(input()); //将接收的参数赋值给变量
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $map = array();
            //关键字查询
            if(isset($keyword) && $keyword!="")
            {
                $map['title'] = ['like',"%" . $keyword . "%"];
            }
            //关键字查询
            try {
                $count = Db::name('auth_group')->where($map)->count();  //总条目数据
                $lists = Model('AuthGroup')->page($NowPage,$limits)->where($map)->select();  //当前页
            }catch (\Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage()]);
            }
            return json(['code' => 200, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }else {
            $this->rule_index();
            return $this->fetch();
        }
    }

    public function group_index_add()
    {
        if(request()->isAjax()){
            $data = input('post.');
            $group['title'] = $data['title'];
            $group['rules'] = get_checkbox_value($data['rule']);
                       try{
                $_add_res = Model('AuthGroup')->save($group);
            }catch (\Exception $e) {
                return show('200',$e->getMessage(),'0');
            }
            if(!$_add_res) {
                return show('200','数据库错误','0');
            }
            return show('200','添加成功，刷新父页面生效','1');
        }else {
            $auth_rule = Model('AuthRule')->getAllSecNav('1');
            $this->assign([
                'auth_rule' => $auth_rule,
            ]);
            $this->rule_index();
            return $this->fetch();
        }
    }

    public function group_index_edit()
    {
        if(request()->isAjax()) {
            $data = input('post.');
            $group['title'] = $data['title'];
            $group['rules'] = get_checkbox_value($data['rule']);
            try{
                $_add_res = Model('AuthGroup')->save($group,['id'=>$data['id']]);
            }catch (\Exception $e) {
                return show('200',$e->getMessage(),'0');
            }
            if(!$_add_res) {
                return show('200','数据库错误','0');
            }
            return show('200','修改成功，刷新父页面生效','1');
        }else{
            $id = Request::instance()->param('id');
            $this_data = Model('AuthGroup')->get(['id'=>$id]);
            $select_data = explode(',',$this_data['rules']);
            $this->assign([
                'this_data' => $this_data,
                'select_data' => $select_data
            ]);
            $this->group_index_add();
            $this->rule_index();
            return $this->fetch();
        }
    }

    public function group_index_del()
    {
        if(request()->isAjax()) {
            $id = Request::instance()->param('id');
            try{
                $res = Model('AuthGroup')->del_msg($id);
            }catch (\Exception $e){
                return show('200',$e->getMessage(),'0');
            }
            if (!$res) {
                return show('200','数据库错误','0');
            }
            return show('200','删除成功！','1');
        }else{
            $this->rule_index();
            return show('500','非法操作');
        }
    }

    public function access_index()
    {
        if(request()->isAjax()){
            extract(input()); //将接收的参数赋值给变量
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $map = array();
            //关键字查询
            if(isset($keyword) && $keyword!="")
            {
                $map['title'] = ['like',"%" . $keyword . "%"];
            }
            //关键字查询
            try {
                $count = Db::name('auth_group_access')->where($map)->count();  //总条目数据
                $lists = Model('AuthGroupAccess')->page($NowPage,$limits)->where($map)->select();  //当前页
            }catch (\Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage()]);
            }
            return json(['code' => 200, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }else {
            $this->rule_index();
            return $this->fetch();
        }
    }

    public function access_index_add()
    {
        if(request()->isAjax()){
            $data = input('post.');
            try{
                $_add_res = Model('AuthGroupAccess')->save($data);
            }catch (\Exception $e) {
                return show('200',$e->getMessage(),'0');
            }
            if(!$_add_res) {
                return show('200','数据库错误','0');
            }
            return show('200','添加成功，刷新父页面生效','1');
        }else {
            $user_list = Model('User')->get_status_all_msg(1);
            $group_list = Model('AuthGroup')->get_status_all_msg(1);
            $this->assign([
                'user_list' => $user_list,
                'group_list' => $group_list
            ]);
            $this->rule_index();
            return $this->fetch();
        }
    }

    public function access_index_edit()
    {
        if(request()->isAjax()) {
            $data = input('post.');
            try{
                $_add_res = Model('AuthGroup')->save($data,['uid'=>$data['uid']]);
            }catch (\Exception $e) {
                return show('200',$e->getMessage(),'0');
            }
            if(!$_add_res) {
                return show('200','数据库错误','0');
            }
            return show('200','修改成功，刷新父页面生效','1');
        }else{
            $uid = Request::instance()->param('uid');
            $this_data = Model('AuthGroupAccess')->get(['uid'=>$uid]);
            $this->assign([
                'this_data' => $this_data,
            ]);
            $this->access_index_add();
            $this->rule_index();
            return $this->fetch();
        }
    }

    public function access_index_del()
    {
        if(request()->isAjax()) {
            $id = Request::instance()->param('uid');
            try{
                $res = Model('AuthGroupAccess')->where(['uid'=>$id])->delete();
            }catch (\Exception $e){
                return show('200',$e->getMessage(),'0');
            }
            if (!$res) {
                return show('200','数据库错误','0');
            }
            return show('200','删除成功！','1');
        }else{
            $this->rule_index();
            return show('500','非法操作');
        }
    }

}